%% @author wangming
%% @doc @todo Add description to jinhua_sangong.
%%
%%金花三公
-module(jinhua_sangong).
-include("common.hrl").
-export([start/0]).
-define(CARD_SORT_BAOZI,6).
-define(CARD_SORT_JINHUASHUN,5).
-define(CARD_SORT_JINHUA,4).
-define(CARD_SORT_SHUNZI,3).
-define(CARD_SORT_DUIZI,2).
-define(CARD_SORT_SANPAI,1).

get_all() ->
	[
	 #{color => 1,val => 1},#{color => 2,val => 1},#{color => 3,val => 1},#{color => 4,val => 1},
	 #{color => 1,val => 2},#{color => 2,val => 2},#{color => 3,val => 2},#{color => 4,val => 2},
	 #{color => 1,val => 3},#{color => 2,val => 3},#{color => 3,val => 3},#{color => 4,val => 3},
	 #{color => 1,val => 4},#{color => 2,val => 4},#{color => 3,val => 4},#{color => 4,val => 4},
	 #{color => 1,val => 5},#{color => 2,val => 5},#{color => 3,val => 5},#{color => 4,val => 5},
	 #{color => 1,val => 6},#{color => 2,val => 6},#{color => 3,val => 6},#{color => 4,val => 6},
	 #{color => 1,val => 7},#{color => 2,val => 7},#{color => 3,val => 7},#{color => 4,val => 7},
	 #{color => 1,val => 8},#{color => 2,val => 8},#{color => 3,val => 8},#{color => 4,val => 8},
	 #{color => 1,val => 9},#{color => 2,val => 9},#{color => 3,val => 9},#{color => 4,val => 9},
	 #{color => 1,val => 10},#{color => 2,val => 10},#{color => 3,val => 10},#{color => 4,val => 10},
	 #{color => 1,val => 11},#{color => 2,val => 11},#{color => 3,val => 11},#{color => 4,val => 11},
	 #{color => 1,val => 12},#{color => 2,val => 12},#{color => 3,val => 12},#{color => 4,val => 12},
	 #{color => 1,val => 13},#{color => 2,val => 13},#{color => 3,val => 13},#{color => 4,val => 13}	
	].


rand_cards(_Cards,GetCards,[]) ->GetCards;
rand_cards(Cards,GetCards,[Ply_sid | Next] ) ->
	{Surplus_Cards,Plyscard} = get_rand_player_card(Ply_sid,Cards,[]),
     rand_cards(Surplus_Cards,GetCards++Plyscard,Next).

get_rand_player_card(Ply_sid,Surplus_Cards,Getcards) when length(Getcards) == 3 ->{Surplus_Cards,[{Ply_sid,Getcards}]};
get_rand_player_card(Ply_sid,Surplus_Cards,Getcards) ->
   Index = util:rand(1, erlang:length(Surplus_Cards)),
   Card = lists:nth(Index, Surplus_Cards),
   get_rand_player_card(Ply_sid,Surplus_Cards -- [Card],[Card | Getcards]).



%%开始
start()->
	Plys = fun_room:get_players(),
	L = san_gong:rand_cards(get_all(),[],Plys),
	{SanGongInfo,JinHuaSanGongInfo,SanGongScoreList,JinHuaSanGongScoreList} = jie_suan_cards(L),
	{ok,?GAME_TYPE_JINHUA,SanGongInfo,JinHuaSanGongInfo,SanGongScoreList,JinHuaSanGongScoreList}.

%%随机到牌后结算 
jie_suan_cards(List) -> 
	Fun = fun({Sid,Getcards},{L,L1}) ->
				  case san_gong:check_card_multiple(Getcards) of
					  {Cardtype,Card_multiple} ->  %%普通三公的牌型和倍数
						case get_card_type(List) of
							JinHuaSanGongMultiole when is_integer(JinHuaSanGongMultiole) andalso JinHuaSanGongMultiole>0 ->
								  case fun_room:get_player(Sid) of
									  #room_ply{aid =Aid } ->
										  {[{Aid,Cardtype,Card_multiple,JinHuaSanGongMultiole}] ++L,[{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Getcards}]++L1};
									  _ -> {L,L1}
								  end;
							_->{L,L1}
						end;
					  _ ->{L,L1}
				  end
		  end,
	{LInfo,L1Info} = lists:foldl(Fun, {[],[]}, List),
	Scorelist = san_gong:check_card_win(LInfo),	
	{LInfo,L1Info,Scorelist,than_a_card(L1Info)}.

  

%%金花比较

%%获得这个牌的牌型
get_card_type(List)->
	case List of
		[#{color :=  Color1,val := Val1},#{color :=  Color2,val := Val2},#{color :=  Color3,val := Val3}|_]->
			CentreVal = lists:nth(2, lists:usort([Val1,Val2,Val3])),
			if Color1 == Color2 andalso Color2 == Color3->
				   if CentreVal *2 == Val1 + Val3 ->
						  ?CARD_SORT_JINHUASHUN;  %%金花顺
					  true->
						  ?CARD_SORT_JINHUA %%金花
				   end;
			   Val1 == Val2 andalso Val2 == Val3 ->
				   ?CARD_SORT_BAOZI; %%豹子
			   CentreVal *2 == Val1 + Val3 ->
					  ?CARD_SORT_SHUNZI;
			   Val1 == Val2 orelse Val1 == Val3 orelse Val2 == Val3->
				   ?CARD_SORT_DUIZI; %%对子
			   true->
				   ?CARD_SORT_SANPAI %%散牌
			end;
		_->0
	end.
		
%%获得牌型的倍数
get_card_mulriple(List)->
	 get_card_type(List).

%%没有庄家比牌规则
no_zhuang_info({Aid,JinHuaSanGongMultiole,Getcards},[],List)->
	List;
no_zhuang_info({Aid,JinHuaSanGongMultiole,CardInfo},[{TagAid,TagJinHuaSanGongMultiole,TagCardInfo}|NextList],ClearingList)->
	[#{color :=  Color1,val := Val1},#{color :=  Color2,val := Val2},#{color :=  Color3,val := Val3}|_] = CardInfo,
	[#{color :=  TagColor1,val := TagVal1},#{color :=  TagColor2,val := TagVal2},#{color :=  TagColor3,val := TagVal3}|_] = TagCardInfo,
	TagStake = san_gong:get_stake(TagAid),
	Stake = san_gong:get_stake(Aid),
	if JinHuaSanGongMultiole == TagJinHuaSanGongMultiole->
		{Color1,Max1} =lists:nth(3, lists:keysort(2, [{Color1,Val1},{Color2,Val2},{Color3,Val3}])),
		{Color2,Max2} = lists:nth(3, lists:keysort(2,[{TagColor1,TagVal1},{TagColor2,TagVal2},{TagColor3,TagVal3}])), 
		case san_gong:check_card_da_or_xiao({Color1,Max1},{Color2,Max2}) of
			?ZHUANG_DA->
				ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Stake*JinHuaSanGongMultiole}];
			_->
				ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,-(TagStake*TagJinHuaSanGongMultiole)}]
		end;
	   JinHuaSanGongMultiole > TagJinHuaSanGongMultiole->
			ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Stake*JinHuaSanGongMultiole}];
	   true->
		   ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,-(TagStake*TagJinHuaSanGongMultiole)}]
	end,
	no_zhuang_info({Aid,JinHuaSanGongMultiole,CardInfo},NextList, ClearingList).


zhuang_info(_ZhuangId,_Info,[],ClearingList)->
	ClearingList;
zhuang_info(ZhuangId,{Aid,JinHuaSanGongMultiole,CardInfo},TagInfoList,ClearingList)->
	[{TagAid,TagJinHuaSanGongMultiole,TagCardInfo}|NextList] = TagInfoList,
	if ZhuangId == Aid->
		   [#{color :=  Color1,val := Val1},#{color :=  Color2,val := Val2},#{color :=  Color3,val := Val3}|_] = CardInfo,
		   [#{color :=  TagColor1,val := TagVal1},#{color :=  TagColor2,val := TagVal2},#{color :=  TagColor3,val := TagVal3}|_] = TagCardInfo,
		   TagStake = san_gong:get_stake(TagAid),
		   Stake = san_gong:get_stake(Aid),
		   if JinHuaSanGongMultiole == TagJinHuaSanGongMultiole->
				  {Color1,Max1} =lists:nth(3, lists:keysort(2, [{Color1,Val1},{Color2,Val2},{Color3,Val3}])),
				  {Color2,Max2} = lists:nth(3, lists:keysort(2,[{TagColor1,TagVal1},{TagColor2,TagVal2},{TagColor3,TagVal3}])), 
				  case san_gong:check_card_da_or_xiao({Color1,Max1},{Color2,Max2}) of
					  ?ZHUANG_DA->
						  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Stake*JinHuaSanGongMultiole}];
					  _->
						  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,-(TagStake*TagJinHuaSanGongMultiole)}]
				  end;
			  JinHuaSanGongMultiole > TagJinHuaSanGongMultiole->
				  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Stake*JinHuaSanGongMultiole}];
			  true->
				  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,-(TagStake*TagJinHuaSanGongMultiole)}]
		   end,
		   zhuang_info(ZhuangId,{Aid,JinHuaSanGongMultiole,CardInfo},NextList, ClearingList);
	   true->
		   case lists:keyfind(ZhuangId, 1, TagInfoList) of
			   {TagAid,TagJinHuaSanGongMultiole,TagCardInfo}->
				   [#{color :=  Color1,val := Val1},#{color :=  Color2,val := Val2},#{color :=  Color3,val := Val3}|_] = CardInfo,
				   [#{color :=  TagColor1,val := TagVal1},#{color :=  TagColor2,val := TagVal2},#{color :=  TagColor3,val := TagVal3}|_] = TagCardInfo,
				   TagStake = san_gong:get_stake(TagAid),
				   Stake = san_gong:get_stake(Aid),
				   if JinHuaSanGongMultiole == TagJinHuaSanGongMultiole->
						  {Color1,Max1} =lists:nth(3, lists:keysort(2, [{Color1,Val1},{Color2,Val2},{Color3,Val3}])),
						  {Color2,Max2} = lists:nth(3, lists:keysort(2,[{TagColor1,TagVal1},{TagColor2,TagVal2},{TagColor3,TagVal3}])), 
						  case san_gong:check_card_da_or_xiao({Color1,Max1},{Color2,Max2}) of
							  ?ZHUANG_DA->
								  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Stake*JinHuaSanGongMultiole}];
							  _->
								  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,-(TagStake*TagJinHuaSanGongMultiole)}]
						  end;
					  JinHuaSanGongMultiole > TagJinHuaSanGongMultiole->
						  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Stake*JinHuaSanGongMultiole}];
					  true->
						  ClearingList ++ [{Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,-(TagStake*TagJinHuaSanGongMultiole)}]
				   end,
				   zhuang_info(ZhuangId,{Aid,JinHuaSanGongMultiole,CardInfo},[], ClearingList);
			   _->[]
		   end
	end.

%%比牌
than_a_card(ListInfo)->
	case get(zhuang) of
		0 ->
			Fun = fun(Info,Acc)->
						 Acc ++ no_zhuang_info(Info,ListInfo--[Info],[])
				  end,
			composite_score(lists:foldl(Fun, [], ListInfo));
		Zhuang->
			Fun1 = fun(Info,Acc)->
						 Acc ++ zhuang_info(Zhuang,Info,ListInfo--[Info],[])
				  end,
			composite_score(lists:foldl(Fun1, [], ListInfo))
	end.


%%综合分数
composite_score(InfoList)->
	Fun = fun({Aid,Multiole,CardType,Stake},Acc)->
				  case lists:keyfind(Aid, 1, Acc) of
					  {Aid,Multiole,CardType,NewStake}->
						  lists:keyreplace(Aid, 1, Acc, {Aid,NewStake+Stake});
					  _->
						  lists:append(Acc, [{Aid,Stake}])
				  end
		  end,
	lists:foldl(Fun, [], InfoList).
	
